import urllib.request
import urllib.parse
import re
import time

# post_fields = {'anfDatum': '30.06.17', 'anfUhrzeit': '06:00', 'endDatum': '30.06.17', 'endUhrzeit': '23:00',
#                'radioLineType': "o.zpkt in ('ZQFA_100')", 'baaSubmit': 'SEND'}

#
# data = urllib.parse.urlencode(post_fields).encode('ascii')
# url = 'http://wwipsw1988/baa/jsp/IPS-C/NEP/system_filling/baa_w76_daily_volume.jsp?language=en&country=GB&winId=1&packageUser=apipsc76:IPS_QFC'
# req = urllib.request.urlopen(url, data)
# content = req.read().decode('iso-8859-1')
#
# total_val_list = re.findall(">(\d+)</td>\\n\\n\\n   </tr>", content, flags=re.S)
# ptotal_val = 0
# if len(total_val_list):
#     total_val = total_val_list[0]
# print(total_val)


def crawl_info(post_data):
    data = urllib.parse.urlencode(post_data).encode('ascii')
    url = 'http://wwipsw1988/baa/jsp/IPS-C/NEP/system_filling/baa_w76_daily_volume.jsp?language=en&country=GB&winId=1&packageUser=apipsc76:IPS_QFC'
    req = urllib.request.urlopen(url, data)
    content = req.read().decode('iso-8859-1')
    total_val_list = re.findall(">(\d+)</td>\\n\\n\\n   </tr>", content, flags=re.S)
    total_val = 0

    print('content')
    print(content)
    if len(total_val_list):
        total_val = total_val_list[0]
    return total_val


def get_date_str(num=0):
    now_timestamp = time.time() + (num * 24 * 3600)
    now_time_tuple = time.localtime(now_timestamp)
    now_date_str = time.strftime('%d.%m.%y', now_time_tuple)
    return now_date_str


def get_shifts_info(date_str=None):
    q_line_shift_1_start_time = '05:54'
    q_line_shift_1_end_time = '14:30'
    q_line_shift_2_start_time = '14:30'
    q_line_shift_2_end_time = '23:00'
    p_line_shift_1_start_time = '05:54'
    p_line_shift_1_end_time = '05:54'
    if date_str is None:
        today_date_str = get_date_str()
        q_date_str = today_date_str
        p_start_date_str = today_date_str
        p_end_date_str = get_date_str(1)
    else:
        time_tuple = time.strptime(date_str, '%Y-%m-%d')
        _date_str = time.strftime('%d.%m.%y', time_tuple)
        q_date_str = _date_str
        p_start_date_str = _date_str
        timestamp = time.mktime(time_tuple) + 24 * 3600
        next_day_time_tuple = time.localtime(timestamp)
        p_end_date_str = time.strftime('%d.%m.%y', next_day_time_tuple)

    line_query_list = [{
        'id': 'qShift1',
        'radioLineType': "o.zpkt in ('ZQFA_100')",
        'anfDatum': q_date_str,
        'anfUhrzeit': q_line_shift_1_start_time,
        'endDatum': q_date_str,
        'endUhrzeit': q_line_shift_1_end_time,
        'baaSubmit': 'SEND'
    }, {
        'id': 'qShift2',
        'radioLineType': "o.zpkt in ('ZQFA_100')",
        'anfDatum': q_date_str,
        'anfUhrzeit': q_line_shift_2_start_time,
        'endDatum': q_date_str,
        'endUhrzeit': q_line_shift_2_end_time,
        'baaSubmit': 'SEND'
    }, {
        'id': 'pShift1',
        'radioLineType': "o.zpkt in ('ZPFA_100')",
        'anfDatum': p_start_date_str,
        'anfUhrzeit': p_line_shift_1_start_time,
        'endDatum': p_end_date_str,
        'endUhrzeit': p_line_shift_1_end_time,
        'baaSubmit': 'SEND'
    }]

    result_dict = {}
    for line_query_item in line_query_list:
        item_id = line_query_item['id']
        del line_query_item['id']
        result_dict[item_id] = crawl_info(line_query_item)
    print(result_dict)

get_shifts_info('2017-07-03')
# print(crawl_info(post_fields))
